var factoryVideoOSArr = new Array();
var factoryVideoOSIDArr = new Array();
var factoryVideoNameArr = new Array();
var factoryVideoIDArr = new Array();
var factoryVideoOSOrderArr = new Array();
var factoryVideoNameOrderArr = new Array();
var factoryVideoOSSel;
var factoryVideoNameSel;
var factoryVideoTypeSel;
var factoryVideoPeroidSel;

var phoneTypes = new Array();
var phoneTypesWPid  = new Array();
var osTypes = new Array();
var factoryVideoDates = new Array();
var factoryVideoData = new Array();

function loadFactoryVideoList(data) {
	factoryVideoOSArr = new Array();
	factoryVideoOSIDArr = new Array();
	factoryVideoNameArr = new Array();
	factoryVideoIDArr = new Array();
	factoryVideoOSOrderArr = new Array();
	factoryVideoNameOrderArr = new Array();
	factoryVideoOSSel = document.getElementById("FactoryVideoOS");
	factoryVideoNameSel  = document.getElementById("FactoryVideoFactory");
	factoryVideoTypeSel  = document.getElementById("FactoryVideoType");

//	factoryVideoPeroidSel  = document.getElementById("factoryVideoPeroidSel");
	factoryVideoOSSel.options.length = 0;
	factoryVideoNameSel.options.length = 0;
	factoryVideoOSSel.options.add(new  Option("全部", ""));
	factoryVideoNameSel.options.add(new  Option("全部", ""));

	$(data.items).each(function(i, value){
		var fOS = value["os"];
		var fOSID = value["osid"];
		var fName = value["fname"];
		var fID = value["fid"];
		var ftype = value["type"];
		var fosi = in_array(fOS, factoryVideoOSArr);
		if(fosi == -1) {
			factoryVideoOSArr.push(fOS);
			factoryVideoOSIDArr.push(fOSID);
			fosi=factoryVideoOSArr.length-1;
			factoryVideoOSOrderArr[fosi] = new Array();
			factoryVideoOSOrderArr[fosi][0] = new Array(); //厂商ID
			factoryVideoOSOrderArr[fosi][1] = new Array(); //厂商名
			factoryVideoOSSel.options.add(new  Option(fOS, fOSID));
		}
		var fnamei = in_array(fID, factoryVideoOSOrderArr[fosi][0])
		if (fnamei == -1)
		{
			factoryVideoOSOrderArr[fosi][0].push(fID);
			factoryVideoOSOrderArr[fosi][1].push(fName);
		}

		var fnamei = in_array(fName, factoryVideoNameArr);
		if(fnamei == -1) {
			factoryVideoNameArr.push(fName);
			factoryVideoIDArr.push(fID);
			fnamei=factoryVideoNameArr.length-1;
			factoryVideoNameOrderArr[fnamei] = new Array();
			factoryVideoNameOrderArr[fnamei][0] = new Array(); //OSID
			factoryVideoNameOrderArr[fnamei][1] = new Array(); //OS名
			factoryVideoNameSel.options.add(new  Option(fName, fID));
		}
		fosi = in_array(fOSID, factoryVideoNameOrderArr[fnamei][0]);
		if (fosi == -1)
		{
			factoryVideoNameOrderArr[fnamei][0].push(fOSID);
			factoryVideoNameOrderArr[fnamei][1].push(fOS);

		}		
	});

	factoryVideoTypeSel.selectedIndex = 0;
	loadFactoryVideoDeviceList();
	getFactoryVideoData();
	jQuery('#body-wrapper').hideLoading();
}


//改变厂商名选项
function changeFactoryVideoFactory(obj) {
	var factoryVideoSelOS = factoryVideoOSSel.options[factoryVideoOSSel.selectedIndex].text;
	var fnamei = in_array(obj.options[obj.selectedIndex].text, factoryVideoNameArr);
	var fosi = factoryVideoOSSel.selectedIndex;

	factoryVideoOSSel.options.length=0;
	factoryVideoOSSel.options.add(new  Option("全部", ""));

	//如果厂商选择的不是全部
	if(fnamei != -1) {
		for(i=0; i<factoryVideoNameOrderArr[fnamei][0].length; i++) {
			factoryVideoOSSel.options.add(new  Option(factoryVideoNameOrderArr[fnamei][1][i], factoryVideoNameOrderArr[fnamei][0][i]));
			if (factoryVideoSelOS == factoryVideoNameOrderArr[fnamei][1][i]) 
				factoryVideoOSSel.selectedIndex = factoryVideoOSSel.options.length -1;
		}
	}
	//如果厂商选择的是全部
	else {
		for(i=0; i<factoryVideoOSArr.length; i++) {
			factoryVideoOSSel.options.add(new  Option(factoryVideoOSArr[i], factoryVideoOSIDArr[i]));
			if (factoryVideoSelOS == factoryVideoOSArr[i]) 
				factoryVideoOSSel.selectedIndex = factoryVideoOSSel.options.length -1;				
			
		}
	}
	loadFactoryVideoDeviceList();

}

//改变平台名选项
function changeFactoryVideoOS(obj) {
	var factoryVideoSelFactory = factoryVideoNameSel.options[factoryVideoNameSel.selectedIndex].text;
	var fosi = in_array(obj.options[obj.selectedIndex].text, factoryVideoOSArr);
	var fnamei = factoryVideoNameSel.selectedIndex;

	factoryVideoNameSel.options.length=0;
	factoryVideoNameSel.options.add(new  Option("全部", ""));

	//如果平台选择的不是全部
	if(fosi != -1) {
		for(i=0; i<factoryVideoOSOrderArr[fosi][0].length; i++) {
			factoryVideoNameSel.options.add(new  Option(factoryVideoOSOrderArr[fosi][1][i], factoryVideoOSOrderArr[fosi][0][i]));
			if (factoryVideoSelFactory == factoryVideoOSOrderArr[fosi][1][i])
				factoryVideoNameSel.selectedIndex = factoryVideoNameSel.options.length -1;			
		}
	}
	//如果平台选择的是全部
	else {
		for(i=0; i<factoryVideoNameArr.length; i++) {
			factoryVideoNameSel.options.add(new  Option(factoryVideoNameArr[i], factoryVideoIDArr[i]));
			if (factoryVideoSelFactory == factoryVideoNameArr[i]) 
				factoryVideoNameSel.selectedIndex = factoryVideoNameSel.options.length -1;
		}
	}
	loadFactoryVideoDeviceList();

}

//改变终端选项
function changeFactoryVideoType(obj) {
	loadFactoryVideoDeviceList();
}

//查询设备列表

function loadFactoryVideoDeviceList(){
	if (factoryVideoOSSel.selectedIndex != 0 && factoryVideoNameSel.selectedIndex != 0 && factoryVideoTypeSel.selectedIndex != 0)
	{
		var api = "http://10.103.13.15:8000/client/factoryDeviceMode?fid=" +  factoryVideoNameSel.options[factoryVideoNameSel.selectedIndex].value +"&osid="+factoryVideoOSSel.options[factoryVideoOSSel.selectedIndex].value + "&type=" +factoryVideoTypeSel.options[factoryVideoTypeSel.selectedIndex].value;
		$.ajax({
			type: "GET",
			url: api,
			dataType : "jsonp",
			jsonnp: "callback",
			success: readFactoryVideoDeviceList
		});
	}
	else {
		$("#FactoryVideoDeviceList").html("<div class=\"ckbText\"><input type=\"checkbox\" id=\"FactoryVideoDeviceAll\" value=\"All\" checked disabled=\"disabled\">全部机型</div>");
	}

}


//生成设备列表
function readFactoryVideoDeviceList(data) {
	var html = "";
	if (data.items.length > 0)
	{
		$(data.items).each(function(i, value){
			var pid = value["pid"];
			var device = value["device"];
			var deviceShort = device;
			if (device.length > 18)
				deviceShort = device.substring(0, 18) + "..."
            if(deviceShort==""){
                   deviceShort = "无对应机型";
            }
			html += "<div style=\"float:left\" class=\"ckbText\"><input type=\"checkbox\" name=\"FactoryVideoDeviceCheckbox\" id=\""+device+"\" value=\""+pid+"\"><span title=\""+device+"\">"+deviceShort+"</span></div>";
		});

	}
	else {
		html = "无对应机型";
	}
	$("#FactoryVideoDeviceList").html(html);

}

//查询统计数据
var FactoryVideoDeviceCheckedItems, FactoryVideoCheckedItems;
function getFactoryVideoData() {
	var startDate = $("#FactoryVideoStartDate").val();
	var endDate = $("#FactoryVideoEndDate").val();
	var osID = document.getElementById("FactoryVideoOS").options[document.getElementById("FactoryVideoOS").selectedIndex].value;
	var typeID = document.getElementById("FactoryVideoType").options[document.getElementById("FactoryVideoType").selectedIndex].value;
	var fID = document.getElementById("FactoryVideoFactory").options[document.getElementById("FactoryVideoFactory").selectedIndex].value;
	$(".FactoryVideoChart").html("");
	if (startDate != "" && endDate != "")
	{
		var items = document.getElementsByName("FactoryVideoDeviceCheckbox");
		var m = 0;
		for(var i=0;i<items.length;i++) {
			if (items[i].checked) 
				m++;
		}

		if (m > 0 || osID == "" || typeID == "" || fID == "") {

			var start = new Date();
			var end = new Date();

			//统计项
			var items = document.getElementsByName("FactoryVideoCheckbox");
			FactoryVideoCheckedItems = new Array();
			for(var i=0;i<items.length;i++) {
				if (items[i].checked)
				{
					FactoryVideoCheckedItems.push(items[i].value);
				}
			}
			if(FactoryVideoCheckedItems.length > 0) {
				jQuery('#body-wrapper').showLoading();
				start.setFullYear(startDate.substring(0,4), startDate.substring(5,7)-1, startDate.substring(8,10));
				end.setFullYear(endDate.substring(0,4), endDate.substring(5,7)-1 ,endDate.substring(8,10));

				days = parseInt((end - start)/1000/60/60/24)+1;

				var APIurl = "http://10.103.13.15:8000/client/factoryVideoAnalytics?staDate="+startDate+"&pd="+days;
				if (osID != "")
					APIurl += "&osid="+osID;
				if (typeID != "")
					APIurl += "&type="+typeID;
				if (fID != "")
					APIurl += "&fid="+fID;

				var items = document.getElementsByName("FactoryVideoDeviceCheckbox");
				FactoryVideoDeviceCheckedItems = new Array();
				for(var i=0;i<items.length;i++) {
					if (items[i].checked) {
						if(FactoryVideoDeviceCheckedItems.length == 0)
							APIurl += "&device=";
						else
							APIurl += ",";
						FactoryVideoDeviceCheckedItems.push(items[i].value);
						APIurl += items[i].value;
					}
				}
				$.ajax({
					type: "GET",
					url: APIurl,
					dataType : "jsonp",
					jsonnp: "callback",
					success: loadFactoryVideoData
				});
			}
			else {
				alert("请选择至少一个统计项");
			}

		}

		else {
			alert("请选择至少一个设备型号");
		}
	}
	else {
		alert("请选择时间区域");
	}
}

var FactoryVideoTabArr, FactoryVideoDetailArr, FactoryVideoSumArr, FactoryVideoDateArr, FactoryVideoSubTitle;
function loadFactoryVideoData(data) {
	jQuery('#body-wrapper').hideLoading();	
	var fID = document.getElementById("FactoryVideoFactory").options[document.getElementById("FactoryVideoFactory").selectedIndex].value;
	var osID = document.getElementById("FactoryVideoOS").options[document.getElementById("FactoryVideoOS").selectedIndex].value;
	var typeID = document.getElementById("FactoryVideoType").options[document.getElementById("FactoryVideoType").selectedIndex].value;
	var tab = "";
	var startDate = $("#FactoryVideoStartDate").val();
	var endDate = $("#FactoryVideoEndDate").val();
	FactoryVideoSubTitle = startDate.replace(/\//g, "-") + " 至 " + endDate.replace(/\//g, "-");

	FactoryVideoTabArr = new Array();
	FactoryVideoDetailArr = new Array();
	FactoryVideoSumArr = new Array();
	FactoryVideoDateArr = new Array();


	//读取数据
	if (data.items.length > 0) //平台选择全部
	{
		if (fID == "") //厂商选择全部
		{	
			changeChartType('FactoryVideo', 'Pie');
			$("#FactoryVideoLineTab").hide();
			$("#FactoryVideoPieTab").show();	
			$("#FactoryVideoLine").html("");
			
			//显示选择Tab
			$("#FactoryVideoTab").html("<div id=\"FactoryVideoTabAll\" class=\"bitem selbitem\">总览</div>");
			//读取数据
			for(n=0; n<FactoryVideoCheckedItems.length; n++) 
				FactoryVideoSumArr[n] = new Array();
			for(i=1; i<document.getElementById("FactoryVideoFactory").options.length; i++) {
				var tabName = document.getElementById("FactoryVideoFactory").options[i].text;
				FactoryVideoTabArr.push(tabName);
				FactoryVideoDetailArr[i-1] = new Array();
			}
			readSummaryData(data, FactoryVideoDateArr, "fname", FactoryVideoTabArr, FactoryVideoDetailArr, FactoryVideoCheckedItems, FactoryVideoSumArr);

			//生成图表
			genTop10Table("FactoryVideoTable", valueFullNameArr(FactoryVideoCheckedItems), FactoryVideoTabArr, FactoryVideoDetailArr, FactoryVideoCheckedItems);
			//生成饼图
			genTop10PieChart(FactoryVideoCheckedItems, "FactoryVideoPie", FactoryVideoDetailArr, FactoryVideoSubTitle, "fname", "厂商Top10");
		}
		else if (osID == "") //平台选择全部
		{
			changeChartType('FactoryVideo', 'Pie');
			$("#FactoryVideoLineTab").hide();
			$("#FactoryVideoPieTab").show();	
			$("#FactoryVideoLine").html("");
			//显示选择Tab
			$("#FactoryVideoTab").html("<div id=\"FactoryVideoTabAll\" class=\"bitem selbitem\">总览</div>");
			//读取数据
			for(n=0; n<FactoryVideoCheckedItems.length; n++) 
				FactoryVideoSumArr[n] = new Array();
			for(i=1; i<document.getElementById("FactoryVideoOS").options.length; i++) {
				var tabName = document.getElementById("FactoryVideoOS").options[i].text;
				FactoryVideoTabArr.push(tabName);
				FactoryVideoDetailArr[i-1] = new Array();
			}
			readSummaryData(data, FactoryVideoDateArr, "os", FactoryVideoTabArr, FactoryVideoDetailArr, FactoryVideoCheckedItems, FactoryVideoSumArr);

			//生成图表
			genTop10Table("FactoryVideoTable", valueFullNameArr(FactoryVideoCheckedItems), FactoryVideoTabArr, FactoryVideoDetailArr, FactoryVideoCheckedItems);
			//生成饼图
			genTop10PieChart(FactoryVideoCheckedItems, "FactoryVideoPie", FactoryVideoDetailArr, FactoryVideoSubTitle, "os", "平台Top10");
		}
		else if (typeID == "") //终端选择为全部
		{
			changeChartType('FactoryVideo', 'Line');
			$("#FactoryVideoPieTab").show();
			$("#FactoryVideoTab").show();
			$("#FactoryVideoLineTab").show();
			//显示选择Tab
			genTabs("FactoryVideo", "FactoryVideoType", FactoryVideoTabArr, FactoryVideoDetailArr, FactoryVideoSumArr, FactoryVideoCheckedItems);
			//读取数据
			readData(data, FactoryVideoDateArr, "type", FactoryVideoTabArr, FactoryVideoDetailArr, FactoryVideoCheckedItems, FactoryVideoSumArr);
			//生成图表
			genSummaryTable("FactoryVideoTable", valueFullNameArr(FactoryVideoCheckedItems), FactoryVideoDateArr, FactoryVideoSumArr, -1, "");
			//生成饼图
			genSelPieChart(FactoryVideoCheckedItems, "FactoryVideoPie", FactoryVideoTabArr, FactoryVideoDetailArr, FactoryVideoSubTitle);
			//生成线形图
			genSummaryLineChart("FactoryVideoLine", "总览趋势图", FactoryVideoDateArr, valueFullNameArr(FactoryVideoCheckedItems), FactoryVideoSumArr, FactoryVideoSubTitle);
		}
		else //厂商、平台、终端均不为全部
		{
			changeChartType('FactoryVideo', 'Line');
			$("#FactoryVideoPieTab").hide();
			$("#FactoryVideoPie").html("");
			$("#FactoryVideoLineTab").show();

			//显示选择Tab
			genDeviceTabs("FactoryVideo", "FactoryVideoDeviceCheckbox", FactoryVideoTabArr, FactoryVideoDetailArr, FactoryVideoSumArr, FactoryVideoCheckedItems);
			//读取数据
			readData(data, FactoryVideoDateArr, "device", FactoryVideoTabArr, FactoryVideoDetailArr, FactoryVideoCheckedItems, FactoryVideoSumArr);

			//生成图表
			genSummaryTable("FactoryVideoTable", valueFullNameArr(FactoryVideoCheckedItems), FactoryVideoDateArr, FactoryVideoDetailArr, 0, FactoryVideoTabArr);
			//生成线形图
			genSummaryLineChart("FactoryVideoLine", "总览趋势图", FactoryVideoDateArr, valueFullNameArr(FactoryVideoCheckedItems), FactoryVideoDetailArr[0], FactoryVideoSubTitle);
		}
	}
	else 
		alert("选择的时间区间内无数据");	
}

function changeFactoryVideoTab(tabName, optionName) {
	if (tabName == "All")
	{
		$("#FactoryVideoPieTab").show();
		$("#FactoryVideoTab").show();

		//生成饼图
		genSelPieChart(FactoryVideoCheckedItems, "FactoryVideoPie", FactoryVideoTabArr, FactoryVideoDetailArr, MainSubTitle);
		//生成线形图
		genSummaryLineChart("FactoryVideoLine", "总览趋势图", FactoryVideoDateArr, valueFullNameArr(FactoryVideoCheckedItems), FactoryVideoSumArr, FactoryVideoSubTitle);
		//生成表格
		genSummaryTable("FactoryVideoTable", valueFullNameArr(FactoryVideoCheckedItems), FactoryVideoDateArr, FactoryVideoSumArr, -1, "");
	}
	else {
		$("#FactoryVideoPieTab").hide();
		$("#FactoryVideoPie").html("");
		$("#FactoryVideoLineTab").show();

		//生成线形图
		genSummaryLineChart("FactoryVideoLine", "总览趋势图", FactoryVideoDateArr, valueFullNameArr(FactoryVideoCheckedItems), FactoryVideoDetailArr[tabName], FactoryVideoSubTitle);
		//生成表格
		genSummaryTable("FactoryVideoTable", valueFullNameArr(FactoryVideoCheckedItems), FactoryVideoDateArr, FactoryVideoDetailArr, tabName, FactoryVideoTabArr);
		if ($("#FactoryVideoPieTab").hasClass("selbitem"))
			changeChartType('FactoryVideo', 'Line');
	} 
	$(".bitem").removeClass("selbitem");
	$("#FactoryVideoTab"+tabName).addClass("selbitem");	
    getCSVData_save("FactoryVideoTable");			
}
